package com.heima.admin.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.services.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    @Override
    public ResponseResult list(SensitiveDto dto) {
        //检查参数
        if (dto == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        IPage pageReq = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.<AdSensitive>lambdaQuery();
        queryWrapper.like(StringUtils.isNoneBlank(dto.getName()), AdSensitive::getSensitives, dto.getName());
        IPage<AdSensitive> page = page(pageReq, queryWrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if (adSensitive == null || StringUtils.isBlank(adSensitive.getSensitives())) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "敏感词不能为空");
        }
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult();

    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if (adSensitive.getId() == null || adSensitive.getId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "敏感词Id不能为空");
        }
        AdSensitive byId = getById(adSensitive.getId());
        if (byId == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "敏感词信息不能为空");
        }
        adSensitive.setCreatedTime(new Date());
        updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "敏感词Id不能为空");
        }
        removeById(id);
        return ResponseResult.okResult();
    }
}
